#
# Makefile
#
CC = arm-linux-gcc
LVGL_DIR_NAME ?= lvgl
LVGL_DIR ?= ${shell pwd}
CFLAGS ?= -O3 -g0 -I$(LVGL_DIR)/ -Wall -Wshadow -Wundef -Wmissing-prototypes \
          -D_GNU_SOURCE -D_DEFAULT_SOURCE -D_BSD_SOURCE \
          -Wextra -Wno-unused-function \
          -Wno-error=strict-prototypes -Wpointer-arith -fno-strict-aliasing \
          -Wno-error=cpp -Wuninitialized -Wno-unused-parameter \
          -Wno-missing-field-initializers -Wtype-limits \
          -Wno-format-nonliteral -Wno-cast-qual -Wunreachable-code \
          -Wno-switch-default -Wreturn-type -Wmultichar -Wformat-security \
          -Wno-ignored-qualifiers -Wno-sign-compare \
          -Wno-error=missing-prototypes -Wdouble-promotion -Wclobbered \
          -Wdeprecated -Wempty-body -Wno-unused-value \
          -std=c99
LDFLAGS ?= -lm -lfreetype

# 定义构建目录
BUILD_DIR = build
BIN_DIR = $(BUILD_DIR)/bin
OBJ_DIR = $(BUILD_DIR)/obj
BIN = $(BIN_DIR)/demo

prefix ?= /usr
bindir ?= $(prefix)/bin

# 收集文件以进行编译
MAINSRC = ./main/src/main.c

include $(LVGL_DIR)/lvgl/lvgl.mk
include $(LVGL_DIR)/lv_drivers/lv_drivers.mk

# 新增c文件在此处添加
CSRCS +=$(LVGL_DIR)/main/src/mouse_cursor_icon.c
CSRCS += $(LVGL_DIR)/main/src/sys_fun.c
CSRCS += $(LVGL_DIR)/main/src/goods.c

OBJEXT ?= .o

# 修改对象文件路径
AOBJS = $(ASRCS:%.S=$(OBJ_DIR)/%$(OBJEXT))
COBJS = $(CSRCS:%.c=$(OBJ_DIR)/%$(OBJEXT))
MAINOBJ = $(MAINSRC:%.c=$(OBJ_DIR)/%$(OBJEXT))

SRCS = $(ASRCS) $(CSRCS) $(MAINSRC)
OBJS = $(AOBJS) $(COBJS)

# 创建构建目录
$(shell mkdir -p $(BIN_DIR) $(OBJ_DIR))

all: default

# 编译规则，确保目录存在
$(OBJ_DIR)/%$(OBJEXT): %.c
	@mkdir -p $(dir $@)
	@$(CC) $(CFLAGS) -c $< -o $@
	@echo "CC $< -> $@"

$(OBJ_DIR)/%$(OBJEXT): %.S
	@mkdir -p $(dir $@)
	@$(CC) $(CFLAGS) -c $< -o $@
	@echo "AS $< -> $@"

default: $(AOBJS) $(COBJS) $(MAINOBJ)
	$(CC) -o $(BIN) $(MAINOBJ) $(AOBJS) $(COBJS) $(LDFLAGS)
	@echo "Build complete: $(BIN)"

clean: 
	rm -rf $(BUILD_DIR)

install:
	install -d $(DESTDIR)$(bindir)
	install $(BIN) $(DESTDIR)$(bindir)

uninstall:
	$(RM) -r $(addprefix $(DESTDIR)$(bindir)/,$(notdir $(BIN)))